<template>
  <div id="homePage">
    <!-- 图片搜索 -->
    <div class="search-bar">
      <!--      走马灯组件-->
      <a-carousel autoplay :effect="'fade'" :style="{ height: '200px' }">
        <div v-for="(item, index) in banners" :key="index">
          <div class="carousel-image" :style="{ backgroundImage: `url(${item.image})` }">
            <!-- 搜索栏定位容器 -->
            <div class="search-wrapper">
              <a-input-search
                v-model:value="searchParams.searchText"
                placeholder="从海量图片中搜索"
                enter-button="搜索"
                size="large"
                @search="doSearch"
              />
            </div>
          </div>
        </div>
      </a-carousel>
    </div>
    <!-- 分类和标签 -->
    <a-tabs v-model:activeKey="selectedCategory" @change="doSearch">
      <a-tab-pane key="all" tab="全部" />
      <a-tab-pane v-for="category in categoryList" :key="category" :tab="category" />
    </a-tabs>
    <div class="tag-bar">
      <span style="margin-right: 8px">标签：</span>
      <a-space :size="[0, 8]" wrap>
        <a-checkable-tag
          v-for="(tag, index) in tagList"
          :key="tag"
          v-model:checked="selectedTagList[index]"
          @change="doSearch"
        >
          {{ tag }}
        </a-checkable-tag>
      </a-space>
    </div>
    <!--      主内容：图片展示-->
    <PictureList :data-list="dataList" :loading="loading" />
    <a-pagination
      style="text-align: right"
      v-model:current="searchParams.current"
      v-model:pageSize="searchParams.pageSize"
      :total="total"
      @change="onPageChange"
    />
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import {
  listPictureTagCategoryUsingGet,
  listPictureVoByPageUsingPost,
} from '@/api/pictureController'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import PictureList from '@/components/PictureList.vue'

const router = useRouter()

//获取图片数据接收
const dataList = ref([]) //记录图片数组
const total = ref(0) // 总图片条数
const loading = ref(true)
// 搜索条件参数
const searchParams = reactive<API.PictureQueryRequest>({
  current: 1,
  pageSize: 12,
  sortField: 'createTime',
  sortOrder: 'descend',
})

// 获取后端图片数据
const fetchData = async () => {
  loading.value = true
  // 设置标签请求参数
  const params = {
    ...searchParams,
    tags: [],
  }
  // 如果分类选择改变，传递对应参数
  if (selectedCategory.value !== 'all') {
    params.category = selectedCategory.value
  }
  // 如果标签选择改变，传递对应参数
  selectedTagList.value.forEach((useTag, index) => {
    if (useTag) {
      params.tags.push(tagList.value[index])
    }
  })

  const res = await listPictureVoByPageUsingPost(params)
  if (res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
  loading.value = false
}

// 让页面第一次刷新时获取一次，之后不必重复获取
onMounted(() => {
  fetchData()
})

const onPageChange = (page: number, pageSize: number) => {
  searchParams.current = page
  searchParams.pageSize = pageSize
  fetchData()
}

/**
 * 图片标题和内容匹配搜索方法
 */
const doSearch = () => {
  searchParams.current = 1 // 不管当前在第几页，默认回到第一页开始搜索
  fetchData()
}

/**
 * 标签和分类
 */
const categoryList = ref<string[]>()
const selectedCategory = ref<string>('all')

const tagList = ref<string[]>([])
const selectedTagList = ref<string[]>([])

const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    categoryList.value = res.data.data.categoryList ?? []
    tagList.value = res.data.data.tagList ?? []
  } else {
    message.error('加载分类标签失败' + res.data.message)
  }
}

onMounted(() => {
  getTagCategoryOptions()
})

// 弹窗引用
const batchEditPictureModalRef = ref()

//批量编辑成功后，刷新数据
const onBatchEditPictureSuccess = () => {
  fetchData()
}

/** 打开批量编辑菜单 */
const doBatchEdit = () => {
  if (batchEditPictureModalRef.value) {
    batchEditPictureModalRef.value.openModal()
  }
}

/**
 * 轮播控制优化
 */

const imgPre = 'https://ziqi-1307175277.cos.ap-shanghai.myqcloud.com/asset'

// 轮播图
const banners = [
  { image: imgPre + '/10001.png', color: '#1890ff' },
  { image: imgPre + '/10002.png', color: '#52c41a' },
]

// 响应式宽度调整
const screenWidth = ref(window.innerWidth)
window.addEventListener('resize', () => {
  screenWidth.value = window.innerWidth
})
</script>

<style scoped>
#homePage {
}

#homePage .search-bar {
  position: relative;
  overflow: hidden;
  border-radius: 10px;
  width: 100%;
  height: 155px;
}

#homePage .search-bar .carousel-image {
  height: 155px;
  max-width: 100%;
  max-height: 100%;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: top;
  position: relative;
}

/* 半透明遮罩层 */
#homePage .search-bar .carousel-image::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
}

#homePage .search-bar .search-wrapper {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 2;
  width: 60%;
  min-width: 50%;
}

/* 覆盖Ant Design默认样式 */
#homePage .search-bar .search-wrapper .ant-input-search {
  border-radius: 40px;
  overflow: hidden;
}

#homePage .search-bar .search-wrapper .ant-input-search .ant-input {
  height: 48px;
  font-size: 16px;
}

#homePage .search-bar .search-wrapper .ant-input-search .ant-btn {
  height: 48px;
  padding: 0 30px;
}

@media (max-width: 400px) {
  .search-wrapper {
    width: 90%;
  }

  .search-wrapper .ant-input-search .ant-input {
    height: 40px;
    font-size: 14px;
  }

  .search-wrapper .ant-btn {
    height: 40px;
    padding: 0 20px;
  }
}

#homePage .tag-bar {
  margin-bottom: 16px;
}
</style>
